﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms

Public Class Test
    Inherits System.Web.UI.Page

    Dim conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("PhonebookConnectionString"))
    'Appsettings is a tag added in Web.config, it's deprecated to put connection string there

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If Not PreviousPage Is Nothing Then
        '    Dim previousControl As TextBox
        '    previousControl = CType(PreviousPage.FindControl("TextBox1"), TextBox)
        'End If
        'If Not previousControl Is Nothing Then
        '    User.Text = previousControl.Text
        'End If
        User.Text = Session("LogonUserID")
        'Record the old zip value
        OldZipHiddenField.Value = ZipTextBox.Text
        Dim sqlCommand As SqlCommand
        Dim selectCommand As String = "select * from contacts;"
        Dim selectScalarCommand As String = "select count(*) from contacts;"
        Dim reader As SqlDataReader
        Dim errorMsg As String = ""
        sqlCommand = New SqlCommand(selectCommand, conn)
        Try
            conn.Open()
            reader = sqlCommand.ExecuteReader()
            GridView1.DataSource = reader
            GridView1.DataBind()
            reader.Read()
            lnameLabel.Text = reader("lname")
            fnameLabel.Text = reader("fname")
            reader.Close()
            sqlCommand.Dispose()
            sqlCommand = New SqlCommand(selectScalarCommand, conn)
            Dim numberOfContacts As Object = sqlCommand.ExecuteScalar
            countLabel.Text = numberOfContacts
        Catch ex As Exception
            errorMsg = ex.Message.ToString
            'Response.Write(errorMsg)
        End Try
        sqlCommand.Dispose()
        conn.Close()
        'Session("initial") = "initial"
    End Sub

    'Added to register the client postback event
    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Page.ClientScript.RegisterForEventValidation(Save.UniqueID)
        MyBase.Render(writer)
    End Sub


    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Response.Write("<script language=javascript> document.getElementById('HiddenField1').value = confirm('Hello World!').toString();</script>")
        If HiddenField1.Value.Equals("True") Then
            Label1.Text = "You pressed OK!"
            'Exit Sub
        Else
            Label1.Text = "You pressed Cancel!"
        End If
        MessageBox.Show("I am executed before you can make a choice!", _
        "Error Occured", _
        MessageBoxButtons.YesNoCancel, _
        MessageBoxIcon.Error)
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs)
        Response.ContentType = "text/plain"
        Dim filename = "Download"
        Response.AddHeader("Content-Disposition", "attachment; filename=""" & filename & """")
        Response.Write("Hello")
    End Sub

    Protected Sub Insert_Click(sender As Object, e As EventArgs) Handles Insert.Click

        Dim msgBoxResponse As DialogResult
        msgBoxResponse = MessageBox.Show("Are you sure you want to proceed?", _
                 "Proceed", _
                 MessageBoxButtons.YesNo, _
                 MessageBoxIcon.Question)
        If msgBoxResponse = DialogResult.Yes Then
            Label2.Text = "You chose OK"
            'Put what you want to do next here.
            'Dim sqlCommand As SqlCommand
            'Dim insertCommand As String = "INSERT INTO CONTACTS (FNAME,LNAME,CELL,PICTURE) VALUES ('TEST', 'TEST', '404-435-6705','UNAVAILABLE')"
            'Try
            '    conn.Open()
            '    sqlCommand = New SqlCommand(insertCommand, conn)
            '    Dim rowsEffected As Integer = sqlCommand.ExecuteNonQuery()

            'Catch ex As Exception
            '    Dim errorMsg As String = ex.Message
            '    Response.Write(errorMsg)
            'End Try
        Else
            Label2.Text = "You chose Cancel"
            Exit Sub
        End If
    End Sub

    Protected Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
        Label3.Text = "Saved"
    End Sub

    Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub

    Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim cookie As HttpCookie = Request.Cookies("ASP.NET_SessionId")
        If Not cookie Is Nothing Then
            SessionID.Text = cookie.Value
        Else
            SessionID.Text = "Not Found"
        End If


    End Sub


End Class